package com.forum.daoImpl;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.forum.dao.ItemDAO;
import com.forum.pojo.Subitem;
import com.forum.pojo.User;
import com.forum.util.BaseDAOImpl;

@Component("itemDAO")
public class ItemDAOImpl extends BaseDAOImpl implements ItemDAO{

	private Subitem subitem;
	private String sql;
	private List list;
	
	public List<String> retrieveItem(){
		
		sql = "select item.itemname,item.user.userName,item.profile,item.postnum,item.clicknum from Subitem item";
		return this.getHibernateTemplate().find(sql);
	}
	
	public List<String> retrieveItem_name(){
		
		sql = "select item.itemname from Subitem item group by item.itemid";
		return this.getHibernateTemplate().find(sql);
	}
	
	public String createItem(Subitem subitem,String owner) {

		sql = "from User u where u.userName = '" + owner + "'";
		User user = null;
		try {
			user = (User)this.getHibernateTemplate().find(sql).get(0);
			if(user != null){
				subitem.setUser(user);
				subitem.setProfile("");
				subitem.setClicknum(0);
				subitem.setPostnum(0);
				this.getHibernateTemplate().save(subitem);
				list = this.retrieveItem();
			}else
				return "error";
		} catch (Exception e) {
			list = null;
			e.printStackTrace();
		}
		return "success";
	}
	
	
	public boolean deleteItem(String itemName) {
		// TODO Auto-generated method stub
		sql = "from Subitem item where item.itemname = '" + itemName + "'";
		try {
			subitem = (Subitem)this.getHibernateTemplate().find(sql).get(0);
			this.getHibernateTemplate().delete(subitem);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean updateItem(String itemName,String m_content,String flag) {
		// TODO Auto-generated method stub
		User user = null;
		sql = "from Subitem item where item.itemname = '" + itemName + "'";
		try {
			subitem = (Subitem)this.getHibernateTemplate().find(sql).get(0);
			if(flag.equals("0"))//flag ==0 就是修改 板块名
				subitem.setItemname(m_content);
			else if(flag.equals("1")){  //flag ==1 就是修改 版主
				sql = "from User u where u.userName = '" + m_content + "'";
				user = (User)this.getHibernateTemplate().find(sql).get(0);
				if(user != null)
					subitem.setUser(user);
				else
					return false;
			}else{ //flag == 2 就是修改简介
				subitem.setProfile(m_content);
			}
			this.getHibernateTemplate().update(subitem);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public Subitem retrieveItemMore(String itemName) {
		// TODO Auto-generated method stub
		sql = "from Subitem item where item.itemname = '" + itemName + "'";
		try {
			subitem = (Subitem)this.getHibernateTemplate().find(sql).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return subitem;
	}
	
	public boolean updateItemMore(Subitem newSubitem) {
		// TODO Auto-generated method stub
		
		sql = "from Subitem item where item.itemname = '" + newSubitem.getItemname() + "'";
		try {
			subitem = (Subitem)this.getHibernateTemplate().find(sql).get(0);
			subitem.setProfile(newSubitem.getProfile());
			//subitem.set
			this.getHibernateTemplate().update(subitem);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 以下是get和set方法的啦，以及我们的构造方法的啦
	 * 
	 * 
	 */
	public ItemDAOImpl() {}

	

	
}
